package org.estela.comp;

import java.io.Reader;
import java.io.StringReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import antlr.RecognitionException;
import antlr.TokenStreamException;
import antlr.collections.AST;

public class EstelaCompiler {

	private final static Log _log = LogFactory.getLog(EstelaCompiler.class);
	
	public static EstelaProgram compile(String str) throws EstelaException {
		//_log.info("compiling: " + str);
		return compile(new StringReader(str));
	}
	
	public static EstelaProgram compile(Reader reader) throws EstelaException {
		EstelaProgram program = null;
		try {

			EstelaLexer lexer = new EstelaLexer(reader);
			EstelaParser parser = new EstelaParser(lexer);
			
			parser.program();
			AST ast = parser.getAST();
			
			EstelaWalker walker = new EstelaWalker();
			program = walker.program(ast);
			
		} catch (RecognitionException ex) {
			throw new EstelaException("Unable to compile:" + reader,  ex);
		} catch (TokenStreamException ex) {
			throw new EstelaException("Unable to compile:" + reader,  ex);
		}
		
		return program;
	}
}
